package com.cl.mycontroller;

import com.cl.annotation.ClComponent;
import com.cl.component.Cat;
import com.cl.component.Dog;
import com.cl.component.Person;
import com.cl.container.MyContainer;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//servlet 注入不了
//@ClComponent(isBean = true,value = "TestServlet")
@WebServlet("/test")
public class TestServlet extends HttpServlet {

    @ClComponent(value = "Dog")
    private Dog dog;

    @ClComponent("Person")
    private Person person;

    @ClComponent("Cat")
    private Cat cat;

    @Override
    public void init() throws ServletException {
        System.out.println(" init  ");
        //在servlet初始化时,注入此类所有的bean (按道理来讲,servlet是单例多线程的,用成员变量会导致线程不安全))
        //就类似 springMvc 中的 controller 中的成员变量bean默认也都是单例的,并不安全
        MyContainer.injectServletBean(this.getClass(), this);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println(dog.getCat().getAge());
        System.out.println(person.getDog());
        System.out.println(person.getCat().getAge());
        person.getCat().shout();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
